package app.beit.fintrack.service.summarytrack;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import app.beit.fintrack.bean.summary.SummaryTrackAppealBean;
import app.beit.fintrack.bean.summary.SummaryTrackChartBean;
import app.beit.fintrack.bean.summary.SummaryTrackDetailsDataBean;
import app.beit.fintrack.bean.summary.SummaryTrackByStatusBean;
import app.beit.fintrack.bean.summary.SummaryTrackPidDataBean;
import app.beit.fintrack.bean.summary.SummaryZoneBean;
import app.beit.fintrack.dao.summarytrack.SummaryTrackDao;
import app.beit.fintrack.entity.summary.SummaryTrackAppeal;
import app.beit.fintrack.entity.summary.SummaryTrackChart;
import app.beit.fintrack.entity.summary.SummaryTrackDetailsData;
import app.beit.fintrack.entity.summary.SummaryTrackByStatus;
import app.beit.fintrack.entity.summary.SummaryTrackPidData;
import app.beit.fintrack.entity.summary.SummaryZone;
import app.beit.fintrack.service.convert.SummaryTrackAppealConvert;
import app.beit.fintrack.service.convert.SummaryTrackChartConvert;
import app.beit.fintrack.service.convert.SummaryTrackDetailsDataConvert;
import app.beit.fintrack.service.convert.SummaryTrackByStatusConvert;
import app.beit.fintrack.service.convert.SummaryTrackPIDConvert;
import app.beit.fintrack.service.convert.SummaryZoneConvert;
import app.beit.fintrack.utils.enums.ESearchByStatus;

public class SummaryTrackService {
	
	private final Logger log = Logger.getLogger(this.getClass());

	@Autowired
	private SummaryTrackDao summaryTrackDao;
	
	public synchronized List<SummaryZoneBean> getSumTotalProcess(final Date fromDate, final Date toDate) throws Exception{
		this.log.info("summary total from e-claim");
		final List<SummaryZone> entities = this.summaryTrackDao.getSumTotalProcess(fromDate, toDate);
		if(entities != null && entities.size() > 0){
			return SummaryZoneConvert.entityToBean(entities);
		}
		return null;
	}

	public synchronized List<SummaryZoneBean> getSumTotalPayable(final Date fromDate, final Date toDate) throws Exception{
		this.log.info("summary total from sapbo payable");
		final List<SummaryZone> entities = this.summaryTrackDao.getSumTotalPayable(fromDate, toDate);
		if(entities != null && entities.size() > 0){
			return SummaryZoneConvert.entityToBean(entities);
		}
		return null;
	}

	public synchronized List<SummaryZoneBean> getSumTotalTransfer(final Date fromDate, final Date toDate) throws Exception{
		this.log.info("summary total from sapbo transfer");
		final List<SummaryZone> entities = this.summaryTrackDao.getSumTotalTransfer(fromDate, toDate);
		if(entities != null && entities.size() > 0){
			return SummaryZoneConvert.entityToBean(entities);
		}
		return null;
	}
	
	public List<SummaryTrackChartBean> getRepNoByHospitalCodeProcessCheckCompensation(final String hospitalCode, final Date fromDate, final Date toDate, 
			final String repNo, final String authType) throws Exception {
		this.log.info("get repno by hospital code step compensation");
		final List<SummaryTrackChart> entities = this.summaryTrackDao.getRepNoByHospitalCodeProcessCheckCompensation(hospitalCode, fromDate, toDate, repNo, authType);
		if(entities != null && entities.size() > 0){
			return SummaryTrackChartConvert.entityToBean(entities);
		}		
		
		return null;
	}
	
	public List<SummaryTrackChartBean> getRepNoByHospitalCodeProcessPetition(final String hospitalCode, final Date fromDate, final Date toDate, 
			final String repNo, final String authType) throws Exception {
		this.log.info("get repno by hospital code step petition");
		final List<SummaryTrackChart> entities = this.summaryTrackDao.getRepNoByHospitalCodeProcessPetition(hospitalCode, fromDate, toDate, repNo, authType);
		if(entities != null && entities.size() > 0){
			return SummaryTrackChartConvert.entityToBean(entities);
		}		
		
		return null;
	}
	
	public List<SummaryTrackChartBean> getRepNoByHospitalCodeProcessBudget(final String hospitalCode, final Date fromDate, final Date toDate, 
			final String repNo, final String authType) throws Exception {
		this.log.info("get repno by hospital code step budget");
		final List<SummaryTrackChart> entities = this.summaryTrackDao.getRepNoByHospitalCodeProcessBudget(hospitalCode, fromDate, toDate, repNo, authType);
		if(entities != null && entities.size() > 0){
			return SummaryTrackChartConvert.entityToBean(entities);
		}		
		
		return null;
	}
	
	public List<SummaryTrackChartBean> getRepNoByHospitalCodePayable(final String hospitalCode, final Date fromDate, final Date toDate, 
			final String repNo, final String authType) throws Exception {
		this.log.info("get repno by hospital code step payable");
		final List<SummaryTrackChart> entities = this.summaryTrackDao.getRepNoByHospitalCodePayable(hospitalCode, fromDate, toDate, repNo, authType);
		if(entities != null && entities.size() > 0){
			return SummaryTrackChartConvert.entityToBean(entities);
		}		
		
		return null;
	}
	
	public List<SummaryTrackChartBean> getRepNoByHospitalCodeTransfer(final String hospitalCode, final Date fromDate, final Date toDate, 
			final String repNo, final String authType) throws Exception {
		this.log.info("get repno by hospital code step transfer");
		final List<SummaryTrackChart> entities = this.summaryTrackDao.getRepNoByHospitalCodeTransfer(hospitalCode, fromDate, toDate, repNo, authType);
		if(entities != null && entities.size() > 0){
			return SummaryTrackChartConvert.entityToBean(entities);
		}		
		
		return null;
	}
	
	public List<SummaryTrackDetailsDataBean> getDetailsByRepNo(final String hospitalCode, final String repNo, final Date fromDate, final Date toDate, 
			final int start, final int end, final String searchBy, final String searchValue) throws Exception {
		this.log.info("get details data from repno");
		final List<SummaryTrackDetailsData> entities = this.summaryTrackDao.getDetailsByRepNo(hospitalCode, repNo, fromDate, toDate, start, end, searchBy, searchValue);
		if(entities != null && entities.size() > 0){
			return SummaryTrackDetailsDataConvert.entityToBean(entities);
		}
		
		return null;
	}
	
	public List<SummaryTrackPidDataBean> getDetailsByPID(final String hospitalCode, final String searchBy, final String value, final Date dateFromAdmidTime, 
			final Date dateToAdminTime, final int start, final int end) throws Exception {
		this.log.info("get details data from type");
		final List<SummaryTrackPidData> entities = this.summaryTrackDao.getDetailsByPID(hospitalCode, searchBy, value, dateFromAdmidTime, dateToAdminTime, start, end);
		if(entities != null && entities.size() > 0){
			return SummaryTrackPIDConvert.entityToBean(entities);
		}
		
		return null;
	}

	public SummaryTrackAppealBean getDataByTranId(final String tranId) throws Exception{
		this.log.info("get data by tran id : " + tranId);
		final SummaryTrackAppeal entity = this.summaryTrackDao.getDataByTranId(tranId);
		if(entity != null){
			return SummaryTrackAppealConvert.entityToBean(entity);
		}		
		
		return null;
	}

	public List<SummaryTrackByStatusBean> getBatchTransferFromRegup(final String hospitalCode, final String repNo) throws Exception {
		this.log.info("get data regup : " + hospitalCode);
		final List<SummaryTrackByStatus> entities = this.summaryTrackDao.getBatchTransferFromRegup(hospitalCode, repNo);
		if(entities != null && entities.size() > 0){
			return SummaryTrackByStatusConvert.entityToBean(entities);
		}		
		
		return null;
	}

	public List<SummaryTrackByStatusBean> getBatchTransferFromZIFEbudgetPostDateNull(final String hospitalCode, final String repNo) throws Exception {
		this.log.info("get data zif ebudget post date null : " + hospitalCode);
		final List<SummaryTrackByStatus> entities = this.summaryTrackDao.getBatchTransferFromZIFEbudgetPostDateNull(hospitalCode, repNo);
		if(entities != null && entities.size() > 0){
			return SummaryTrackByStatusConvert.entityToBean(entities);
		}		
		
		return null;
	}

	public List<SummaryTrackByStatusBean> getBatchTransferFromZIFEbudgetPostDateNotNull(final String hospitalCode, final String repNo) throws Exception {
		this.log.info("get data zif ebudget post date not null : " + hospitalCode);
		final List<SummaryTrackByStatus> entities = this.summaryTrackDao.getBatchTransferFromZIFEbudgetPostDateNotNull(hospitalCode, repNo);
		if(entities != null && entities.size() > 0){
			return SummaryTrackByStatusConvert.entityToBean(entities);
		}		
		
		return null;
	}
	

	
	public List<SummaryTrackDetailsDataBean> getDetailsByRepNo(final String hospitalCode, final String repNo, final Date fromDate, final Date toDate) throws Exception {
		this.log.info("get details data from repno");
		final List<SummaryTrackDetailsData> entities = this.summaryTrackDao.getDetailsByRepNo(hospitalCode, repNo, fromDate, toDate);
		if(entities != null && entities.size() > 0){
			return SummaryTrackDetailsDataConvert.entityToBean(entities);
		}
		
		return null;
	}
	
	public BigDecimal getSumsActAmount(final String hospitalCode, final String repNo, 
			final Date fromDate, final Date toDate, final String searchBy, final String searchValue)throws Exception{
		return this.summaryTrackDao.getSumsActAmount(hospitalCode, repNo, fromDate, toDate, searchBy, searchValue);
	}
	
	public BigDecimal getSumsTotal(final String hospitalCode, final String repNo, 
			final Date fromDate, final Date toDate, final String searchBy, final String searchValue)throws Exception{
		return this.summaryTrackDao.getSumsTotal(hospitalCode, repNo, fromDate, toDate, searchBy, searchValue);
	}
	
	public BigDecimal getCountPerson(final String hospitalCode, final String repNo, 
			final Date fromDate, final Date toDate, final String searchBy, final String searchValue, 
			final ESearchByStatus status)throws Exception{
		return this.summaryTrackDao.getCountPerson(hospitalCode, repNo, fromDate, toDate, searchBy, searchValue, status);
	}
}
